package com.yhm.myfootbollshow.demos.service.serviceimpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhm.myfootbollshow.demos.entity.po.Cart;
import com.yhm.myfootbollshow.demos.entity.po.Product;
import com.yhm.myfootbollshow.demos.entity.po.User;
import com.yhm.myfootbollshow.demos.entity.vo.CartVo;
import com.yhm.myfootbollshow.demos.mapper.CartMapper;
import com.yhm.myfootbollshow.demos.mapper.ProductMapper;
import com.yhm.myfootbollshow.demos.mapper.UserMapper;
import com.yhm.myfootbollshow.demos.service.CartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: yhm
 * @time: 2025/4/15 20:48
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private UserMapper userMapper;
    @Override
    public List<CartVo> list(Long userId) {
        // 构造器构造条件
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(Cart::getUserId, userId);
        // 查询购物车信息
        List<Cart> carts = baseMapper.selectList(wrapper);
        // 创建购物车视图列表
        ArrayList<CartVo> cartVos = new ArrayList<>();
        // 遍历购物车信息
        for (Cart cart : carts) {
            CartVo cartVo = new CartVo();
            BeanUtil.copyProperties(cart,cartVo);
            // 查询商品信息
            Product product = productMapper.selectById(cart.getProductId());
            // 组装购物
            User user = userMapper.selectById(cart.getUserId());
            cartVo.setProductName(product.getProductName());
            cartVo.setUsername(user.getUsername());
            cartVo.setProductPrice(product.getPrice());
            cartVo.setProductImage(product.getMainImage());
            cartVos.add(cartVo);
        }
        return cartVos;
    }
}
